package com.four.collage.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.collage.dao.AttributeMapper;
import com.four.collage.entity.Attribute;
import com.four.collage.entity.Groupgoods;
import com.four.collage.dao.GroupgoodsMapper;
import com.four.collage.service.IGroupgoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.collage.vo.GoodsAndAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-18
 */
@Service
public class GroupgoodsServiceImpl extends ServiceImpl<GroupgoodsMapper, Groupgoods> implements IGroupgoodsService {

    @Autowired
    private GroupgoodsMapper groupgoodsMapper;

    @Autowired
    private AttributeMapper attributeMapper;


    @Override
    public void addGroupGoods(GoodsAndAttrVo goodsAndAttrVo) {
        //添加拼团商品
        Groupgoods groupgoods = new Groupgoods();
        groupgoods.setManIcon(goodsAndAttrVo.getManIcon());
        groupgoods.setGrPicture(goodsAndAttrVo.getGrPicture());
        groupgoods.setGrName(goodsAndAttrVo.getGrName());
        groupgoods.setGrContent(goodsAndAttrVo.getGrContent());
        groupgoods.setGrConpany(goodsAndAttrVo.getGrConpany());
        groupgoods.setStartTime(goodsAndAttrVo.getStartTime());
        groupgoods.setEndTime(goodsAndAttrVo.getEndTime());
        groupgoods.setGrPrescription(goodsAndAttrVo.getGrPrescription());
        groupgoods.setGrSize(goodsAndAttrVo.getGrSize());
        groupgoods.setGrSort(goodsAndAttrVo.getGrSort());
        groupgoods.setGrPurchaseQuantityLimit(goodsAndAttrVo.getGrPurchaseQuantityLimit());
        groupgoods.setGrFeightTempalte(goodsAndAttrVo.getGrFeightTempalte());
        groupgoods.setGrStatus(goodsAndAttrVo.getGrStatus());
        groupgoodsMapper.addGroupGoods(groupgoods);
        //添加拼团商品属性
        Attribute attribute = new Attribute();
        attribute.setAttAttribute(goodsAndAttrVo.getAttAttribute());
        attribute.setAttPicture(goodsAndAttrVo.getAttPicture());
        attribute.setAttOriginalPrice(goodsAndAttrVo.getAttOriginalPrice());
        attribute.setAttStock(goodsAndAttrVo.getAttStock());
        attribute.setAttGroupPrice(goodsAndAttrVo.getAttGroupPrice());
        attribute.setAttLimit(goodsAndAttrVo.getAttLimit());
        attribute.setAttWeight(goodsAndAttrVo.getAttWeight());
        attribute.setAttVolume(goodsAndAttrVo.getAttVolume());
        attribute.setGrId(groupgoods.getGrId());
        attributeMapper.addAttribute(attribute);
    }

    /**
     * 根据拼团商品id查询信息
     * 修改时回显
     * @param grId
     * @return
     */
    @Override
    public Groupgoods getGroupgoodsById(Integer grId) {
        return groupgoodsMapper.getGroupgoodsById(grId);
    }

    /**
     * 根据拼团商品id修改
     * @param grId
     * @param goodsAndAttrVo
     */
    @Override
    public void updateGroupgoods(Integer grId, GoodsAndAttrVo goodsAndAttrVo) {
        goodsAndAttrVo.setGrId(grId);
        //修改拼团商品
        Groupgoods groupgoods = new Groupgoods();
        groupgoods.setGrId(grId);
        groupgoods.setManIcon(goodsAndAttrVo.getManIcon());
        groupgoods.setGrPicture(goodsAndAttrVo.getGrPicture());
        groupgoods.setGrName(goodsAndAttrVo.getGrName());
        groupgoods.setGrContent(goodsAndAttrVo.getGrContent());
        groupgoods.setGrConpany(goodsAndAttrVo.getGrConpany());
        groupgoods.setStartTime(goodsAndAttrVo.getStartTime());
        groupgoods.setEndTime(goodsAndAttrVo.getEndTime());
        groupgoods.setGrPrescription(goodsAndAttrVo.getGrPrescription());
        groupgoods.setGrSize(goodsAndAttrVo.getGrSize());
        groupgoods.setGrSort(goodsAndAttrVo.getGrSort());
        groupgoods.setGrPurchaseQuantityLimit(goodsAndAttrVo.getGrPurchaseQuantityLimit());
        groupgoods.setGrFeightTempalte(goodsAndAttrVo.getGrFeightTempalte());
        groupgoods.setGrStatus(goodsAndAttrVo.getGrStatus());
        groupgoodsMapper.updateGroupgoods(groupgoods);
        //修改品按商品属性
        Attribute attribute = new Attribute();
        attribute.setAttAttribute(goodsAndAttrVo.getAttAttribute());
        attribute.setAttPicture(goodsAndAttrVo.getAttPicture());
        attribute.setAttOriginalPrice(goodsAndAttrVo.getAttOriginalPrice());
        attribute.setAttStock(goodsAndAttrVo.getAttStock());
        attribute.setAttGroupPrice(goodsAndAttrVo.getAttGroupPrice());
        attribute.setAttLimit(goodsAndAttrVo.getAttLimit());
        attribute.setAttWeight(goodsAndAttrVo.getAttWeight());
        attribute.setAttVolume(goodsAndAttrVo.getAttVolume());
        attribute.setGrId(grId);
        attributeMapper.updateAttribute(attribute);
    }

    /**
     * 根据拼团商品id删除商品
     * @param grId
     */
    @Override
    public void delGroupgoodsById(Integer grId) {
        //删除拼团商品
        groupgoodsMapper.delGroupgoodsById(grId);
        //删除拼团商品属性
        attributeMapper.delAttribute(grId);
    }

    /**
     * 拼团列表、模糊、精准、分页
     *
     * @param vo
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<Groupgoods> groupGoodsList(GoodsAndAttrVo vo, Integer page, Integer size) {

        Page<Groupgoods> groupPage = new Page<>(page, size);
        return groupgoodsMapper.groupGoodsList( groupPage,vo);
    }


}
